package org.zzh.service.system.role;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.zzh.dao.common.PageBean;
import org.zzh.dao.mapper.PeRoleMapper;
import org.zzh.dao.mapper.SsModuleMapper;
import org.zzh.domain.system.role.PeRole;

import java.util.List;

@Service
public class PeRoleServiceImpl implements PeRoleService{

    @Autowired
    private PeRoleMapper peRoleMapper;

    @Autowired
    private SsModuleMapper ssModuleMapper;

    @Override
    public List<PeRole> getUserRoleList(String id) {
        return peRoleMapper.getUserRoleList(id);
    }

    @Override
    public List<PeRole> list() {
        return peRoleMapper.getAll();
    }

    @Override
    public PageBean<PeRole> list(Integer page, Integer pageSize, String name) {
        PageHelper.startPage(page, pageSize);
        List<PeRole> list = peRoleMapper.list(name);
        PageInfo<PeRole> peRolePageInfo = new PageInfo<>(list);
        PageBean<PeRole> pageBean = new PageBean<>();
        pageBean.setData(peRolePageInfo.getList());
        pageBean.setTotalCount((int) peRolePageInfo.getTotal());
        pageBean.setTotalPage(peRolePageInfo.getPages());
        pageBean.setPage(page);
        pageBean.setPageSize(pageSize);
        return pageBean;
    }

    @Override
    public List<String> getUserRoleIds(String id) {
        return peRoleMapper.getUserRoleIds(id);
    }

    @Override
    public PeRole findById(String id) {
        return peRoleMapper.findById(id);
    }

    @Override
    @Transactional
    public void add(PeRole role) {
        role.setCreateTime(new java.util.Date());
        peRoleMapper.add(role);
    }

    @Override
    @Transactional
    public void update(PeRole role) {
        role.setUpdateTime(new java.util.Date());
        peRoleMapper.update(role);
    }

    @Override
    @Transactional
    public void deleteList(String[] ids) {
        peRoleMapper.deleteRoleUserListByRoleIds(ids);
        ssModuleMapper.deleteRoleModuleListByRoleIds(ids);
        peRoleMapper.deleteList(ids);
    }

}
